package com.bite.demo.controller;

import com.bite.demo.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserInfoService userInfoService;

    //@Transactional(rollbackFor = Exception.class)
    //@Transactional(isolation = Isolation.READ_COMMITTED)
    @Transactional(propagation = Propagation.MANDATORY)
    @RequestMapping("/registry")
    public String registry(String name, String password) {
        userInfoService.registryUser(name, password);
        try {
            int a = 10/0;
        }catch(Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return "注册成功";
    }
    //JDBC事务管理器
//    @Autowired
//    private DataSourceTransactionManager dataSourceTransactionManager;
//
//    //定义事务属性
//    @Autowired
//    private TransactionDefinition transactionDefinition;

//    @Autowired
//    private UserInfoService userInfoService;

//    @RequestMapping("/registry")
//    public String registry(String name, String password){
//        //开启事务
//        TransactionStatus transactionStatus = dataSourceTransactionManager
//                .getTransaction(transactionDefinition);
//        //用户注册
//        userInfoService.registryUser(name,password);
////        //提交事务
////        dataSourceTransactionManager.commit(transactionStatus);
//        //回滚事务
//        dataSourceTransactionManager.rollback(transactionStatus);
//        return "注册成功";
//    }
}
